import React from 'react';
import { inject, observer } from 'mobx-react';
import { Button, Spin } from 'antd';
import {toJS} from "mobx";
import {WeaTableNew} from 'comsMobx';
const WeaTable = WeaTableNew.WeaTable;
import {WeaRightMenu,WeaTop,WeaTab,WeaErrorPage,WeaTools,WeaProgress,WeaBrowser,WeaAlertPage} from 'ecCom';
import {Condition,getAdButtons} from '../list/listCondition';
import {WeaLocaleProvider} from 'ecCom';
const getLabel = WeaLocaleProvider.getLabel;

@inject('cptReportStore')
@observer
class CapitalReport extends React.Component {
    constructor(props) {
        super(props);
    }
    componentDidMount(){
        const {cptReportStore,params } = this.props;
        const {initData} = cptReportStore;
        initData(params);
    }
    componentWillReceiveProps(nextProps){
        const keyOld = this.props.location.key;
        const keyNew = nextProps.location.key;
        const oldParams = this.props.params.reptype;
        const newParams = nextProps.params.reptype;
		if(keyOld !== keyNew || oldParams !== newParams) {
            const {cptReportStore,params } = nextProps;
            const {initData} = cptReportStore;
            cptReportStore.setShowSearchAd(false);
            initData(params);
        }
    }
    componentWillUnmount(){
        const {cptReportStore,params } = this.props;
        cptReportStore.clearStatus();
    }
    render(){
        const {cptReportStore} = this.props;
        const {title,loading,form,showSearchAd,tableStore,reLoad,verified,hasRight} = cptReportStore;
        if (verified && !hasRight) {
            return (<WeaAlertPage ecId={`${this && this.props && this.props.ecId || ''}_WeaAlertPage@v61pu1`} >
                    <div style={{color : '#000'}}>
                        {getLabel(2012,'对不起，您暂时没有权限！')}
                    </div>
                </WeaAlertPage>
            )
        }
        if (verified && hasRight) {
            return (
                <WeaRightMenu ecId={`${this && this.props && this.props.ecId || ''}_WeaRightMenu@joa8xy`} datas={this.getRightMenu()}   onClick={this.onRightMenuClick.bind(this)}  
                collectParams={{
                    favname:'',
                    favouritetype: "5",
                    objid: 0,    
                    importantlevel:1,
                }}>
                    <WeaTop ecId={`${this && this.props && this.props.ecId || ''}_WeaTop@gplwrf`}
                        title={title}
                        loading={loading}
                        icon={<i className='icon-coms-fa' />}
                        iconBgcolor='#f14a2d'
                        buttons={[]}
                        buttonSpace={10}
                        showDropIcon={true}
                        dropMenuDatas={this.getRightMenu()}
                        onDropMenuClick={this.onRightMenuClick.bind(this)} 
                    >
                        <WeaTab ecId={`${this && this.props && this.props.ecId || ''}_WeaTab@ij1mbd`}
                            buttonsAd={getAdButtons(cptReportStore)}
                            searchType={['advanced']}
                            setShowSearchAd={bool=>{cptReportStore.setShowSearchAd(bool)}}
                            hideSearchAd={()=> cptReportStore.setShowSearchAd(false)}
                            searchsAd={
                                <div><Condition ecId={`${this && this.props && this.props.ecId || ''}_Condition@kaw42g`} listStore={cptReportStore} form={form} onEnterSearch={this.onEnterSearch}></Condition></div>
                            }
                            showSearchAd={showSearchAd}
                            onSearch={v=>{prjExecuteStore.doSearch()}}
                            advanceHeight={100}
                        />
                        <WeaTable ecId={`${this && this.props && this.props.ecId || ''}_WeaTable@jgx7nq`} 
                            comsWeaTableStore={tableStore}
                            hasOrder={true}
                            needScroll={true}
                            getColumns={c=>this.reRenderColumns(c)}
                            register_table={()=>reLoad()}
                        />
                    </WeaTop>
                </WeaRightMenu>
            )
        }
        return <div style={{position:"absolute",width:"100%",top:"48%",textAlign:"center"}}><Spin ecId={`${this && this.props && this.props.ecId || ''}_Spin@8whke6`} /></div>
    }
    reRenderColumns(columns){
        columns.forEach(c=>{
            if(c.dataIndex=='resultcount'){
                c.render = function(text, record){
                    if(record.resultcountspan){
                        return  <span  className='wea-cpt-progressStyle' >
                            <WeaProgress ecId={`${this && this.props && this.props.ecId || ''}_WeaProgress@lawy54`} percent={record.resultcountspan}   />
                        </span>
                    }
                }
            } else {
              c.render = function(text, record){
                  let valueSpan = record[c.dataIndex + "span"] !== undefined ? record[c.dataIndex + "span"] : record[c.dataIndex];
                  return <span dangerouslySetInnerHTML={{__html: valueSpan}}></span>
                }
            }
        })
        return columns;
    }
    getRightMenu(){
        const {cptReportStore} = this.props;
        const {rightMenu} = cptReportStore;
        let btnArr = [];
        rightMenu && rightMenu.length>0 && rightMenu.map(m=>{
            btnArr.push({
                icon: <i className={m.menuIcon} />,
                content: m.menuName,
            })
        });
    	return btnArr
    }
    onRightMenuClick(key){
        const {cptReportStore} = this.props;
        const {rightMenu} = cptReportStore;
        let that = this;
        rightMenu &&  rightMenu.length>0  && rightMenu.map((m,i)=>{
        	if(Number(key) == i){
                if(m.type == "BTN_SEARCH"){ 
                    cptReportStore.doSearch();
                }
            }
        });
    }

    onEnterSearch = () =>{
        const {cptReportStore} = this.props;
        cptReportStore.doSearch();
        cptReportStore.setShowSearchAd(false);
    }

}

export default WeaTools.tryCatch(React,
    props => <WeaErrorPage ecId={`${this && this.props && this.props.ecId || ''}_WeaErrorPage@z49o1f`} msg={ props.error ? props.error : getLabel( 383324,'对不起，该页面异常，请联系管理员' )} />,
    {error: ""}
)(CapitalReport);